import com.check.Check_CNC_Info;
import com.service.rfid.baluff.BALUFF_RfidServiceImpl;
import com.service.rfid.sygole.SG_RfidServiceImpl;
import com.utils.CRC16Util;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static com.utils.ByteUtil.Bytes2HexString;
import static com.utils.ByteUtil.HexString2Bytes;

public class TestRfid {
    /*加载配置文件*/

//
//    @Test
//    public void TestReadRfid(){
//        System.out.println(new BALUFF_RfidServiceImpl().readRfid());
//    }
//    @Test
//    public void TestWriteRfid(){
//        System.out.println(new BALUFF_RfidServiceImpl().writeRfid("DDDDD303"));
//    }



    @Before
    public void LoadIni(){
        new Check_CNC_Info();
    }

    /*-----------------国产RFID（思谷）----------------*/

    /**
     * 读
     * @throws IOException
     */
    @Test
    public void testRfidRead() throws IOException {
        new SG_RfidServiceImpl().strRead(511);
    }

    /**
     * 写
     * @throws IOException
     */
    @Test
    public void testRfidWrite() throws IOException {
        /*360个字符*/
//        String info = "0217KD10;NUM222;NAMMillTaper;" +
//                "STN4;SIZ0;SED0;DIR0;MON2;DNO1;TYP4;EDP1;" +
//                "OFX0.0;OFY0.0;OFZ0.0;LEX0.0;LEY0.0;LEZ0.0;CUA0.0;" +
//                "MAA0.0;STR0.0;CUR0.0;CUL0.0;TTH0;CLX0.0;CLY0.0;CLZ0.0;" +
//                "CCA0.0;CMA0.0;CSR0.0;CCR0.0;LIF133;TAR333;ALM4443{cca:0.0," +
//                "ofx:0.0,cur:Detect,lex:0.0,lez:0.0,ley:0.0,alm:0.0,dno:1,tar:0.0,siz:0.0," +
//                "stn:1,clx:0.0,cua:0.0,clz:0.0,edp:9,cly:0.0,cma:0.0,typ:}";
//

        /*373*/
        String info = "sangjiexun!!!0217KD10;NUM222;NAMMillTaper;" +
                "STN4;SIZ0;SED0;DIR0;MON2;DNO1;TYP4;EDP1;" +
                "OFX0.0;OFY0.0;OFZ0.0;LEX0.0;LEY0.0;LEZ0.0;CUA0.0;" +
                "MAA0.0;STR0.0;CUR0.0;CUL0.0;TTH0;CLX0.0;CLY0.0;CLZ0.0;" +
                "CCA0.0;CMA0.0;CSR0.0;CCR0.0;LIF133;TAR333;ALM4443{cca:0.0," +
                "ofx:0.0,cur:Detect,lex:0.0,lez:0.0,ley:0.0,alm:0.0,dno:1,tar:0.0,siz:0.0," +
                "stn:1,clx:0.0,cua:0.0,clz:0.0,edp:9,cly:0.0,cma:0.0,typ:}";
        System.out.println(new SG_RfidServiceImpl().strWrite(info));
    }

    @Test
    public void TestRead_SG_Rfid(){
//        System.out.println(new SG_RfidServiceImpl().readUID());

        String f1 = "FF 08 11 00 01";
        String f2 = "00";//id
        String f3 = "00";//块号
        String f4 = "00";//地址
        String f5 = "08";//长度
//        FF 08 11 00 01 00 00 00 08 A5 73
        String reqStr = f1+f2+f3+f4+f5;

        String f6 = CRC16Util.getCRC(reqStr);
        System.out.println(f6.trim().replace(" ",""));

        String f7 = reqStr+f6.trim().replace(" ","");

        System.out.println(f7.trim().replace(" ",""));
//        String s = "00000018A0010098C68E00989A690000000000BC614E000055AA55AA";
        byte[] b = HexString2Bytes(f7.trim().replace(" ",""));
////        byte[] b = HexString2Bytes(reqStr);
//
        System.out.println(Bytes2HexString(b));
    }


    //    @Test
//    public void TestWrite_SG_Rfid(){
//        System.out.println(new BALUFF_RfidServiceImpl().writeRfid("DDDDD303"));
//    }

    @Test
    public void t1(){
//        int starNo = 0;
//        int endNo = 120;
//        String info = "0217KD10;NUM222;NAMMillTaper;STN4;" +
//                "SIZ0;SED0;DIR0;MON2;DNO1;TYP4;EDP1;OFX0.0;OFY0.0;" +
//                "OFZ0.0;LEX0.0;LEY0.0;LEZ0.0;CUA0.0;MAA0.0;STR0.0;CUR0.0;CUL0.0;TTH0;" +
//                "CLX0.0;CLY0.0;CLZ0.0;CCA0.0;CMA0.0;CSR0.0;CCR0.0;LIF133;TAR333;ALM4443";
        String info = "0217KD10;NUM222;NAMMillTaper;" +
                "STN4;SIZ0;SED0;DIR0;MON2;DNO1;TYP4;EDP1;" +
                "OFX0.0;OFY0.0;OFZ0.0;LEX0.0;LEY0.0;LEZ0.0;CUA0.0;" +
                "MAA0.0;STR0.0;CUR0.0;CUL0.0;TTH0;CLX0.0;CLY0.0;CLZ0.0;" +
                "CCA0.0;CMA0.0;CSR0.0;CCR0.0;LIF133;TAR333;ALM4443{cca:0.0," +
                "ofx:0.0,cur:Detect,lex:0.0,lez:0.0,ley:0.0,alm:0.0,dno:1,tar:0.0,siz:0.0," +
                "stn:1,clx:0.0,cua:0.0,clz:0.0,edp:9,cly:0.0,cma:0.0,typ:}";
//        info = info+info;
        int len = info.length();
        int startNo = 0;
        int endNo = 0;
        int n = 0;
        int more =0;
        if(len % 120 !=0) {
            n = len / 120;
            more = len - n * 120;

//            System.out.println(n);
            for (int i = 0; i < n; i++) {
//                System.out.println(i);
                startNo = (1 * i * 120);
                endNo = ((i + 1) * 120 )-1;
                System.out.println(startNo);
                System.out.println(endNo);
                if (i == 0) {
                    System.out.println(info.substring(startNo,endNo));
                } else {
                    System.out.println(info.substring(startNo-1,endNo));
                }

            }

            startNo = endNo+1;
            endNo = more;
            System.out.println(startNo);
            System.out.println(endNo);
            System.out.println(info.substring(startNo-1,startNo+endNo));


        } else {

            n = len / 120;
            for (int i = 0; i < n; i++) {
//                System.out.println(i);
                startNo = (1 * i * 120);
                endNo = ((i + 1) * 120);
                System.out.println(startNo);
                System.out.println(endNo);
//
                if (i == 0) {
                    System.out.println(info.substring(startNo,endNo));
                } else {
                    System.out.println(info.substring(startNo-1,endNo));
                }
//                    String sentData = doEnCode(startNo,120);
//                    System.out.println(sentData);
                //需要发送的报文
//                    System.out.println(info);
            }
        }

    }
}
